package com.abbas.CayTcore.xview

import android.content.Context
import android.text.Editable
import android.text.TextUtils
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.ViewGroup
import android.widget.BaseAdapter
import android.widget.EditText
import android.widget.LinearLayout
import android.widget.ScrollView
import androidx.core.widget.addTextChangedListener
import com.abbas.CayTcore.R
import com.abbas.CayTcore.entity.CounterItem
import kotlinx.android.synthetic.main.activity_add_core_type.view.*
import kotlinx.android.synthetic.main.activity_counter_result.view.*
import kotlinx.android.synthetic.main.fragment_core_detail.view.*

/**
 * ListView里有EditText，解决焦点问题
 *
 * @author abbas
 * @date 2023/04/02
 */
class EditListView(context: Context?, attrs: AttributeSet?) : ScrollView(context, attrs) {
    public var dz: Float = 0f
        set(value) {
            field = value
            initAllData()
        }
    public var callback: ((quantity: Float) -> Unit)? = null
    public var hasPackage: Boolean = false
        set(value) {
            field = value
            initAllData()
        }
    var packageWeight: Float = 0f
//        set(value) {
//            field = value
//            data.forEach {
//                it.much = it.count / dz
//            }
//            initList()
//            refreshData()
//        }

    private val mContentView = LinearLayout(context)
    val data: MutableList<CounterItem> = mutableListOf<CounterItem>(
        CounterItem(1, 0f, 0f)
    )

    var allWeight: Float = 0f
    var allQuantity: Float = 0f

    init {
        mContentView.layoutParams =
            LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT)
        mContentView.orientation = LinearLayout.VERTICAL
        initList()
        addView(mContentView)
    }

    private fun initAllData() {
        if (hasPackage) {
            data.forEach {
                it.much = (it.count - packageWeight) / dz
            }
        } else {
            data.forEach {
                it.much = it.count / dz
            }
        }
        initList()
        refreshData()
    }

    private fun initList() {
        mContentView.removeAllViews()
        data.forEachIndexed { position, it ->
            val itemView =
                LayoutInflater.from(context).inflate(R.layout.counter_rv_item, mContentView, false)
            itemView.layoutParams = LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.MATCH_PARENT,
                LinearLayout.LayoutParams.WRAP_CONTENT
            )
            val mi: EditText = itemView.findViewById(R.id.mi)
            val weight: EditText = itemView.findViewById(R.id.weight)
            val count: EditText = itemView.findViewById(R.id.count)
            mi.text = Editable.Factory.getInstance().newEditable(it.number.toString())
            mi.addTextChangedListener {
                if (!TextUtils.isEmpty(it)) {
                    data[position].number = it.toString().toInt()
                }
            }
            weight.text = Editable.Factory.getInstance().newEditable(it.count.toString())
            weight.addTextChangedListener {
                if (!TextUtils.isEmpty(it)) {
                    it.toString().toFloat().apply {
                        if (hasPackage) {
                            data[position].count = this
                            val much = (this - packageWeight) / dz
                            data[position].much = much
                            count.setText(much.toString())
                        } else {
                            data[position].count = this
                            data[position].much = this / dz
                            count.setText((this / dz).toString())
                        }
                        refreshData()
                    }
                }
            }
            count.text = Editable.Factory.getInstance().newEditable(it.much.toString())

            mContentView.addView(itemView)
        }
    }

    public fun addItem() {
        data.add(
            CounterItem(data.size + 1, 0f, 0f)
        )
        initList()
    }

    public fun getItem(position: Int): CounterItem {
        return data[position]
    }

    public fun getCount(): Int {
        return data.size
    }

    public fun refreshData() {
        allWeight = 0f
        allQuantity = 0f
        data.forEach {
            allWeight += it.count
            allQuantity += it.much
        }
        callback?.invoke(allQuantity)
    }


    public fun notifyDataChanged() {
        initList()
    }
}
